/*
 * Copyright (C), 2002-2017, 苏宁易购电子商务有限公司
 * FileName: ActivityController.java
 * Author:   15050536
 * Date:     2017年3月27日 下午2:48:30
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.web.controller.member;

import java.security.Principal;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.member.dto.Activity;
import com.suning.sawp.member.dto.StoreExpand;
import com.suning.sawp.member.service.ActivityService;
import com.suning.sawp.member.vo.ActivityVo;
import com.suning.sawp.member.vo.CustomerVo;
import com.suning.sawp.member.vo.MemCustomerDetailResponse;
import com.suning.sawp.member.vo.StoreExpandVo;
import com.suning.sawp.web.controller.DjBaseController;
import com.suning.sawp.web.exception.MyGsonView;

/**
 * 新会员外拓
 *
 * @author 15050536 石键平
 */
@Controller
@RequestMapping("/client/activity/")
public class ActivityController extends DjBaseController {

	@Autowired
	private ActivityService activityService;

	/**
	 * 客户端店长新增活动
	 *
	 * @param activity
	 * @param principal
	 */
	@RequestMapping("addActivity")
	public MyGsonView addActivity(@RequestBody ActivityVo activity, Principal principal) {
		String staffId = principal.getName();
		ReturnMsg<Activity> returnMsg = activityService.addActivityByShopKeeper(activity, staffId);
		return setGsonViewWithEmpty(returnMsg);
	}

	/**
	 * 客户端店长删除活动
	 *
	 * @param activity
	 * @param principal
	 */
	@RequestMapping("deleteActivity")
	public MyGsonView deleteActivity(Long activityId, Principal principal) {
		String staffId = principal.getName();
		ReturnMsg<Activity> returnMsg = activityService.deleteActivity(activityId, staffId, true);
		return setGsonViewWithEmpty(returnMsg);
	}

	/**
	 * 修改活动之前，查询活动vo
	 *
	 * @param activity
	 * @param principal
	 */
	@RequestMapping("findActivityDetail4Mod")
	@ResponseBody
	public MyGsonView queryActivityById(@RequestParam("activityId") Long activityId, Principal principal) {
		String staffId = principal.getName();
		ReturnMsg<JSONObject> returnMsg = activityService.findActivityVoById(activityId, staffId);
		return setGsonViewWithEmpty(returnMsg);
	}

	/**
	 * 客户端店长修改活动
	 *
	 * @param activity
	 * @param principal
	 */
	@RequestMapping("modifyActivity")
	public MyGsonView modifyActivity(@RequestBody ActivityVo activityVo, Principal principal) {
		String staffId = principal.getName();
		ReturnMsg<Activity> returnMsg = null;
		if (StringUtils.isEmpty(activityVo.getId())) {
			returnMsg = activityService.addActivityByShopKeeper(activityVo, staffId);
		} else {
			returnMsg = activityService.modifyActivity(activityVo, staffId);
		}
		return setGsonViewWithEmpty(returnMsg);
	}

	/**
	 * 客户端 查询活动列表
	 *
	 * @param status
	 *            0:进行中活动(默认) 1:已结束
	 * @param cateCode
	 *            品类编码
	 * @param startIndex
	 *            分页的起始下标
	 * @return
	 */
	@RequestMapping("queryActivityList")
	public MyGsonView queryActivityList(@RequestParam(defaultValue = "0", required = false) String status, String cateCode, @RequestParam(defaultValue = "0", required = false) int startIndex,
			Principal principal) {
		String staffId = principal.getName();
		ReturnMsg<JSONObject> returnMsg = activityService.queryActivityList(status, cateCode, startIndex, staffId);
		return setGsonViewWithEmpty(returnMsg);
	}

	/**
	 * 客户端 查询活动地点列表
	 *
	 * @param status
	 *            0:进行中活动(默认) 1:已结束
	 * @param cateCode
	 *            品类编码
	 * @param startIndex
	 *            分页的起始下标
	 * @return
	 */
	@RequestMapping("queryActivityExpandList")
	public MyGsonView queryActivityExpandList(Long activityId, String cateCode, @RequestParam(defaultValue = "0", required = false) int startIndex, Long storeCateId, Principal principal) {
		String staffId = principal.getName();
		ReturnMsg<JSONObject> returnMsg = activityService.findActivityExpandStatictisByCate(activityId, cateCode, startIndex, storeCateId, staffId);
		return setGsonViewWithEmpty(returnMsg);
	}

	/**
	 * 
	 * 查询拓展任务的详细内容,修改之前总是要先查一下的呢
	 *
	 * @param expandId
	 * @return
	 */
	@RequestMapping("findActivityExpandDetail")
	public MyGsonView findActivityExpandDetail(Long expandId) {
		ReturnMsg<JSONObject> returnMsg = activityService.findExpandDetailInfo(expandId);
		return setGsonViewWithEmpty(returnMsg);
	}

	/**
	 * 客户端督导 新增或编辑 拓展任务
	 *
	 * @param activity
	 * @param principal
	 */
	@RequestMapping("saveOrUpdateActivityExpand")
	public MyGsonView saveOrUpdateActivityExpand(@RequestBody StoreExpandVo storeExpandVo, Principal principal) {
		String staffId = principal.getName();
		ReturnMsg<StoreExpand> returnMsg = activityService.saveStoreExpand(storeExpandVo, staffId);
		return setGsonViewWithEmpty(returnMsg);
	}

	/**
	 * 客户端督导删除任务
	 *
	 * @param id
	 *            拓展任务id(expandId)
	 * @param principal
	 */
	@RequestMapping("delActivityExpand")
	public MyGsonView delActivityExpand(Long id, Principal principal) {
		String staffId = principal.getName();
		ReturnMsg<StoreExpand> returnMsg = activityService.deleteStoreExpand(id, staffId);
		return setGsonViewWithEmpty(returnMsg);
	}

	/**
	 * 
	 * 查询某门店正在进行中的活动列表
	 *
	 * @return
	 */
	@RequestMapping("queryActiveActivities")
	public MyGsonView queryActiveActivities(Principal principal) {
		String staffId = principal.getName();
		ReturnMsg<JSONObject> returnMsg = activityService.queryActiveActivities(staffId);
		return setGsonViewWithEmpty(returnMsg);
	}

	/**
	 * 
	 * 根据手机号码查询客户信息
	 *
	 * @param mobile
	 *            手机号
	 * @param activityId
	 *            活动id
	 * @param principal
	 * @return
	 */
	@RequestMapping("queryCustomerByPhone")
	public MyGsonView queryCustomerByPhone(String mobile, String source, String activityId, Principal principal) {
		String staffId = principal.getName();
		ReturnMsg<CustomerVo> returnMsg = activityService.queryCustomerByPhone(mobile, source, activityId, staffId);
		return setGsonView(returnMsg);
	}

	/**
	 * 新增或编辑客户资料
	 *
	 * @param customerDetail
	 * @param principal
	 * @return
	 */
	@RequestMapping("saveCustomer")
	public MyGsonView saveCustomer(@RequestBody MemCustomerDetailResponse customerDetail, Principal principal) {
		String staffId = principal.getName();
		ReturnMsg<MemCustomerDetailResponse> returnMsg = activityService.saveOrUpdateCustomer(customerDetail, staffId);
		return setGsonView(returnMsg);
	}

	/**
	 * 店员维度-查询地点列表
	 * 
	 * @param principal
	 * @return
	 */
	@RequestMapping("queryPlaceList4Emp")
	public MyGsonView queryPlaceList4Emp(Long activityId, @RequestParam(defaultValue = "0", required = false) int startIndex, Principal principal) {
		String staffId = principal.getName();
		ReturnMsg<JSONObject> returnMsg = activityService.queryPlaceList4Emp(activityId, staffId, startIndex);
		return setGsonView(returnMsg);
	}

}
